var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var wxUtil = require('../../utils/wxUtil.js');
var WxParse = require('../../utils/wxParse/wxParse.js');
var shopInfo = require('../../components/shopInfo/index.js');
var ShareActionSheet = require('../../components/shareActionSheet/index.js');
var MPage = require('../MPage.js');
var app = getApp();

Page(common.extendPage(MPage, {
	data: {
		dataLoaded: false,
	},

	onShareAppMessage: function() {
		var {
			groupBuyId
		} = this.options;
		var goodsName = '';

		try {
			goodsName = this.data.goodsInfo['goods_name'];
		} catch (e) {}
		return {
			title: goodsName,
			path: '/pages/groupBuyInfo/index?groupBuyId=' + groupBuyId,
		}
	},

	onLoad(options) {
		var _self = this;

		this.options = options = util.parseOptions(options);
		common.initApp({
			loginSuc: function() {
				_self._fetch();
				_self._shareAs = new ShareActionSheet({
					page: _self,
					scope: 'shareActionSheet'
				});
			}
		});
	},

	onPullDownRefresh() {
		var _self = this;
		_self._fetch();
	},

	_showActionSheet() {
		var {
			groupBuyId
		} = this.options;
		var {
			actInfo,
		} = this.data;

		this._shareAs.show({
			type: 3,
			actId: actInfo.id,
			groupBuyId: groupBuyId,
		});
	},

	_fetch() {
		var _self = this;
		var curUserId = app.getUserId();
		var {
			groupBuyId
		} = this.options;

		ajax.G('/openapi/CActivity/join', {
			group_buy_id: groupBuyId
		}, function(data) {
			var result = data.result;
			var {
				actInfo,
				goodsInfo,
				groupBuyId,
				leftTime,
				orderInfo,
				groupBuyState, //0、未成团 1、拼团成功  2、拼团已过期  3、活动已结束
			} = result;
			var {
				joinedUsers
			} = actInfo;
			var allUsers = [];
			var leader;
			var isAllPay = true;
			var isJoined = false;
			var isJoinedPay = false;

			joinedUsers.forEach(function(user) {
				if (user.isOriginUser == 1) {
					leader = user;
				}
				if (user.payState != 1) {
					isAllPay = false;
				}
				if (user.id == curUserId) {
					isJoined = true;
					isJoinedPay = user.payState == 1;
				}
			});

			for (var i = 0; i < actInfo.leftPeopleNum; i++) {
				allUsers.push({});
			}

			allUsers = joinedUsers.concat(allUsers);

			_self.setData({
				dataLoaded: true,
				actInfo,
				goodsInfo,
				allUsers,
				groupBuyId,
				orderInfo,
				groupState: groupBuyState,
				leftTime,
				isLeader: curUserId == leader.id,
				isJoined,
				isJoinedPay,
				isAllPay
			});
			_self._lastTime = isJoined && !isJoinedPay ? orderInfo['left_time'] : leftTime;
			if (leftTime && leftTime > 0) {
				_self._actTimer && clearTimeout(_self._actTimer);
				_self._actTimerDown();
			}
			wx.stopPullDownRefresh();
		});
	},

	_actTimerDown() {
		var _self = this;
		var sec = _self._lastTime;
		var lastTimeStr = util.getTimeBySec(sec, 'd');

		_self.setData({
			lastTimeStr,
		});
		if (sec == 0) {
			_self._actTimer && clearTimeout(_self._actTimer);
			_self._fetch();
			return;
		}
		_self._lastTime--;
		_self._actTimer = setTimeout(function() {
			_self._actTimerDown();
		}, 1000)
	},

	/**
	 * 未支付订单支付
	 */
	_payOrder(evt) {
		var _self = this;
		var {
			orderId,
			groupId
		} = evt.target.dataset;

		ajax.P('/openapi/CActivity/doPayAgain', {
			orderId,
			openId: app.getOpenId(),
		}, function(data) {
			var wxPayParams = JSON.parse(data.result.wxPayParams);
			var _package = wxPayParams.package;
			var prepayArr = _package.split('=');

			common.submitPrepay(prepayArr[1]);

			wxPayParams.success = function() {
				wx.redirectTo({
					url: '/pages/groupBuyInfo/index?groupBuyId=' + groupId
				});

				_self._toTab(1);
			}
			wxUtil.wxPay(wxPayParams);
		});
	},

}));